﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mammoth.Events;

namespace Mammoth.ViewModels
{
    public partial class MainWindowViewModel
    {
        private void InitializeGTalk()
        {
            GTalkService.Message += GTalkService_Message;
            GTalkService.Error += GTalkService_Error;
            GTalkService.SubscriptionRequest += GTalkService_SubscriptionRequest;
        }

        public void LoginAllAccounts()
        {
            Task.Factory.StartNew(GTalkService.LoginAll);
        }

        private void GTalkService_Message(object sender, ServiceMessageEventArgs e)
        {
            
            Dispatcher.RaiseInboxEvent(e.ServiceEmail, e.BuddyEmail, e.Message);
        }

        void GTalkService_SubscriptionRequest(object sender, SubscriptionRequestEventArgs e)
        {
            Dispatcher.RaiseSubscriptionEvent(e.ServiceEmail, e.BuddyEmail);
        }

        void GTalkService_Error(object sender, ServiceErrorEventArgs e)
        {
            switch (e.Error)
            {
                case ServiceErrorType.AuthenticationError:
                    Dispatcher.RaiseErrorEvent(string.Format("Email atau password salah pada {0}", e.ServiceEmail));
                    break;
                case ServiceErrorType.ConnectionError:
                    Dispatcher.RaiseErrorEvent(string.Format("Koneksi ke server gagal pada {0}", e.ServiceEmail));
                    break;
                case ServiceErrorType.InvalidAccount:
                    Dispatcher.RaiseErrorEvent(string.Format("Akun tidak valid pada {0}", e.ServiceEmail));
                    break;
                case ServiceErrorType.AccountExist:
                    Dispatcher.RaiseErrorEvent(string.Format("Akun {0} sudah terdaftar", e.ServiceEmail));
                    break;
            }
        }


    }
}
